<?php 
	require_once "BaseController.php";
	require_once "../Model/Services.php";
	
	class ServiceController extends BaseController
	{
		public function ServiceController()
		{
			parent::__construct( "devkingsapp" );
		//	var_dump( $_GET );
		}
		
		public function addService( $service )
		{
			$query =  "INSERT INTO Services ( city, postalCode, email, telephone, catId, type ) "
					. "VALUES ( '" . $service->cityName . "' , '" . $service->postalCode . "' , '"
							       . $service->email . "' , '" . $service->telNumber . "' , " 
								   . $service->categories[0]->catName . " , '" . $service->type . "' )";

			$this->runQuery( $query );
		}
		
		public function getAllRows()
		{
			$result = array();
			
			$rows = $this->runQuery( "SELECT * FROM Services 
									  INNER JOIN Categories 
									  WHERE Services.catId = Categories.id" );
			
			if ( $rows != NULL )
			{
				while ( $row = mysqli_fetch_array( $rows ) )
				{
					$service = new Services( $row[0], $row[ "type" ], $row["city"], 
					                         $row["postalCode"], $row["email"], 
											 $row["telephone"], $row["name"] );
					array_push( $result, $service );
				}
			
				$this->close();
				
				return $result;
			}
			
			return NULL;
		}
	}
	
	$serviceController = new ServiceController();
	
	//create service using get Data
	$service = new Services( 0, $_POST[ "sType" ], $_POST[ "sCity" ], $_POST[ "sPostalCode" ], 
	                         $_POST[ "sEmail" ], $_POST[ "sTel" ], $_POST[ "sCat" ] );
	$serviceController->addService( $service );
	
	$rows = $serviceController->getAllRows();
	
	var_dump( $rows );
	var_dump( $rows[0]->categories[0]->catName );
?>